<?php
/**
 * String helper utility class.
 *
 * This class includes some Java-like functions for parsing strings,
 * as well as some functions for getting qualifiers / unqualifying phing-style
 * classpaths.  (e.g. "util.StringUtil").
 *
 * @author yipsilon
 * @package util
 */
class StringUtil {

	function isLetter($char) {
		if (strlen($char) !== 1)
		$char = 0;
		$char = (int) ord($char);
		if ($char >= ord('A') && $char <= ord('Z'))
		return true;
		if ($char >= ord('a') && $char <= ord('z'))
		return true;
		return false;
	}

	/**
	 * Converts a string to an indexed array of chars
	 * There's really no reason for this to be used in PHP, since strings
	 * are all accessible using the $string{0} notation.
	 * @param string $string
	 * @return array
	 * @deprecated
	 */
	public static function toCharArray($str) {
		$ret=array();
		$len=strlen($str);
		for ($i=0; $i < $len; $i++) {
			$ret[] = $str{$i};
		}
		return $ret;
	}

	/**
	 * @return int
	 */
	public static function hashCode($string) {
		return crc32($string);
	}

	/** tests if a string starts with a given string */
	public static function startsWith($check, $string) {
		if ($check === "" || $check === $string) {
			return true;
		} else {
			return (strpos($string, $check) === 0) ? true : false;
		}
	}

	/** tests if a string ends with a given string */
	public static function endsWith($check, $string) {
		if ($check === "" || $check === $string) {
			return true;
		} else {
			return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
		}
	}

	public static function substring($string, $start ,$sublen){
		$pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
		preg_match_all($pa, $string, $t_string);
		if(count($t_string[0]) - $start > $sublen){
			return join('', array_slice($t_string[0], $start, $sublen))."...";
		}else{
			return join('', array_slice($t_string[0], $start, $sublen));
		}
	}
}
?>
